#ifndef SUBSCRIPTIONLISTMODEL_H
#define SUBSCRIPTIONLISTMODEL_H


/** Classes related to parsing rss feeds
    and downloading mp3 podcasts, and presenting them as
    QAbstractListModel to QML ListViews.
    @author Alan Ezust
*/

#include <QAbstractListModel>
#include <QUrl>
#include <QDir>
#include <QDateTime>
#include <QNetworkAccessManager>
#include <QXmlStreamReader>
#include <QNetworkReply>
#include "episodes.h"
#include "downloadmgr.h"


/** A model for a list of Podcast Episodes
    that come from an rss feed. 
    Shown in SubscriptionView.qml
    @author Alan Ezust
*/
class ItemsListModel : public QAbstractListModel {
    Q_OBJECT
public:
    enum Roles {NameRole = Qt::UserRole + 1,
                PathRole,
                PictureRole,
                IsAvailableRole,
                DescriptionRole,
                ProgressRole,
                PubDateRole,
                StatusRole,
                UrlRole };
    explicit ItemsListModel(QObject* parent=0);

    QVariant data(const QModelIndex &index, int role) const;
public slots:
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    void setSubscripton(Subscription *s);
    /** The user clicks on this episode. If it is during a current download,
        then the download aborts. On the following click, the download
        is restarted. */
    void downloadEpisode(int i);
    void deleteEpisode (int idx);
protected slots:
    void episodeChanged(Episode e);
    void downloadXmlFinished(QNetworkReply*);
    void parseXml(QString filename);
    void refreshIfNecessary(Subscription* s);
signals:
    void subscriptionUpdated(Subscription* s);
private:
    Episode m_currentReading;
    QList<Episode> m_items;
    Subscription* m_subscription;
    bool m_inDescription;
    QNetworkAccessManager m_manager;
    QXmlStreamReader m_streamReader;
    DownloadMgr m_downloadManager;
};


// --------------------------------------------------------------------------------------------


/** A list of Subscriptions
    Uses subscribed.tsv in your podcast dir.
    Shown in ParentListView.qml
    @author Alan Ezust
*/
class SubscriptionListModel : public QAbstractListModel {
    Q_OBJECT
public:
    enum Roles {
        NameRole = Qt::UserRole + 1,
        UrlRole,
        LastCheckedRole
    };
    /** refresh xml
    @param row if -1, refresh all
    */
    void reload (int row=-1);
    explicit SubscriptionListModel(QObject *parent = 0);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    Subscription &subscription(int row) ;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    bool removeRows(int row, int count, const QModelIndex &parent);
signals:
    /** Signal emitted to mainwindow so it can notify other things
      to update info related to this subscription */
    void subscriptionUpdated(Subscription* s);


public slots:
    /** emit proper dataChanged signal for the row corresponding to this
      subscription */
    void updateSubscription(Subscription* s);

    /** Add a subscription to the list and save. */
    void subscribe (QString name, QUrl url);


protected:
    /** Save back to subscriptionsFile() */
    void save();
    /** Filename to use for subscriptions list. */
    QString subscriptionsFile() const;
    /** Restore from subscriptionsFile() */
    void restore();

    QList<Subscription> m_subscriptions;

};

#endif // SUBSCRIPTIONLISTMODEL_H

